global class JottHandler{

WebService static Messaging.InboundEMailResult doIt(Messaging.InboundEmail email) {
System.debug ('from: '+email.fromAddress);
System.debug ('My HTML: '+email.htmlBody);
System.debug ('My Subject: '+email.subject);
System.debug ('My plain    text: '+email.plainTextBody);

//if (email.subject.equals('Contact'))
{
//Contact[] newContacts=new Contact[0];
    //String inputStr = email.plainTextBody;
    //String patternStr = ',';
    //String[] fields = inputStr.split(patternStr);

//System.debug ('Field: '+fields[0]);
System.debug('Called from JOTT');
String s=email.htmlBody;
Integer match=s.indexOf('<div style="font-family:trebuchet ms;color:#9999ff;font-size:16px;font-weight:bold;padding:15px 10px;background:#fff;\">',0);

// Determine the Subject
Integer mEnd=s.indexOf('<div style="color:#f6f2e9;font-size:12px;border-top:1px solid #d0cdb0;margin-top:7px;padding-top:7px;\">',0);
String sResult=s.substring(match+121, mEnd-7);


// Now get the URL for the recording
match=s.indexOf('<div style="color:#f6f2e9;font-size:12px;border-top:1px solid #d0cdb0;margin-top:7px;padding-top:7px;\"><a href=\"', 0);
mEnd=s.indexOf('\" style=\"color: #f07822;font-size:11px;\" target=\"_blank\">Listen to',0);

//String sTemp=
String sURL=s.substring(match+'<div style="color:#f6f2e9;font-size:12px;border-top:1px solid #d0cdb0;margin-top:7px;padding-top:7px;\"><a href=\"'.length(), mEnd);

//String sResult=s.substring(match+121, match+121+30);

System.debug('Comment=' + sResult);

// Initially create contact - just for test
/*
newContacts.add (new Contact(FirstName='Jott',
LastName='call',
Title=sResult,
Email='hcheng@jott.com'));
insert newContacts;
*/
String myPlainText= '';
// Add the email plain text into the local variable
//myPlainText = email.plainTextBody;
// New Task object to be created
Task[] newTask = new Task[0];
// Try to lookup any contacts based on the email from address
// If there is more than 1 contact with the same email address,
// an exception will be thrown and the catch statement will be called.
// Where Email = :email.fromAddress
 
try {
Contact vCon = [Select Id, Name, Email, AccountId
From Contact
Where Email = 'hcheng@jott.com'
Limit 1];
// Add a new Task to the contact record we just found above.
newTask.add(new Task(Description = sResult + ' URL= ' +sURL,
Priority = 'Normal',
Status = 'Completed',
Subject = email.subject,
IsReminderSet = true,
ReminderDateTime = System.now()+1,
WhatId=  vCon.AccountId,
WhoId = vCon.Id));

// WhatId relates Account, Contract,Case, Opp,
// Insert the new Task and it will be created and appended to the contact record.
insert newTask;
System.debug ('Contact: '+vCon.Name);
System.debug ('AccountID: '+vCon.AccountId);
System.debug ('URL: '+ sURL);
System.debug('New Task Object: ' + newTask );
}
// If there is an exception with the query looking up
// the contact, this QueryException will be called.
// and the exception will be written to the Apex debug log.
catch (QueryException e) {
System.debug('Query Issue: ' + e);
}
}
return new Messaging.InboundEmailResult();
}


}